package com.zxl.designpattern.behavioral.state;

/**
 * 具体状态类：售罄状态
 * 当贩卖机商品售罄时的状态
 */
public class SoldOutState implements State {

    // 贩卖机
    private final VendingMachine vendingMachine;
    
    /**
     * 构造函数，初始化贩卖机引用
     * @param vendingMachine 贩卖机对象
     */
    public SoldOutState(VendingMachine vendingMachine) {
        this.vendingMachine = vendingMachine;
    }
    
    @Override
    public void pay(int amount) {
        System.out.println("商品已售罄，请等待补货");
    }
    
    @Override
    public void selectItem() {
        System.out.println("商品已售罄，请等待补货");
    }
    
    @Override
    public void dispense() {
        System.out.println("商品已售罄，请等待补货");
    }
    
    @Override
    public void refill(int quantity) {
        if (quantity <= 0) {
            System.out.println("补货数量必须大于0");
            return;
        }
        
        // 更新库存
        int newCount = vendingMachine.getCount() + quantity;
        vendingMachine.setCount(newCount);
        vendingMachine.setState(vendingMachine.getSelectionState());
        System.out.println("成功补货 " + quantity + " 件");
        System.out.println("当前库存: " + newCount + " 件");
        System.out.println("现在可以挑选商品了");
    }
}    